package com.example.rest.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: RestTemplate
 * @BelongsPackage: com.example.rest.service
 * @Author:hlzs1
 * @Description: 描述什么人干什么事儿
 * @CreateTime: 2023-02-28 09:04
 * @Version: 1.0
 */
@Service
public class GetService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @description:占位符拼接参数
     * @author: haolizhuo
     * @date: 2023/2/28
     * @param: []
     * @return: java.lang.String
     **/
    public String getForEntity(){
        String url = "http://localhost:8090/restTemplate" +"/hello?name={1}";
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, "郝立琢");

        HttpHeaders headers = response.getHeaders();
        System.out.println("响应头headers===="+headers);

        String responseBody = response.getBody();
        System.out.println("响应体body===="+responseBody);

        HttpStatus statusCode = response.getStatusCode();
        System.out.println("响应状态码====" + statusCode);

        return responseBody;

    }
    /**
     * @description:map传递参数
     * @author: haolizhuo
     * @date: 2023/2/28
     * @param: []
     * @return: java.lang.String
     **/
    public String getForEntity2(){
        String url = "http://localhost:8090/restTemplate" +"/hello?name={name}";
        Map<String,String> map = new HashMap<>();
        map.put("name","map郝立琢");
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class, map);
        return forEntity.getBody();
    }

    /**
     * @description:参数直接写到url地址内
     * @author: haolizhuo
     * @date: 2023/2/28
     * @param: []
     * @return: java.lang.String
     **/
    public String getForEntity3(){
        String url = "http://localhost:8090/restTemplate/hello?name=郝立琢直接拼参";
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        return forEntity.getBody();
    }

    /**
     * @description:
     * @author: 占位符拼接参数,直接获取响应体
     * @date: 2023/2/28
     * @param: []
     * @return: java.lang.String
     **/
    public String getForObject(){
        String url = "http://localhost:8090/restTemplate" +"/hello?name={1}";
        String forObject = restTemplate.getForObject(url, String.class, "郝立琢obj");
        return forObject;
    }

    /**
     * @description:map传递参数
     * @author: haolizhuo
     * @date: 2023/2/28
     * @param: []
     * @return: java.lang.String
     **/
    public String getForObject2(){
        String url = "http://localhost:8090/restTemplate" +"/hello?name={name}";
        Map<String,String> map = new HashMap<>();
        map.put("name","map郝立琢");
        String forObject = restTemplate.getForObject(url, String.class, map);
        return forObject;
    }

    /**
     * @description:参数直接写到url地址内
     * @author: haolizhuo
     * @date: 2023/2/28
     * @param: []
     * @return: java.lang.String
     **/
    public String getForObject3(){
        String url = "http://localhost:8090/restTemplate/hello?name=郝立琢直接拼参";
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }



}
